package com.css.zfzx.fxpg.modules.vulnerability.province.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.zfzx.fxpg.modules.vulnerability.area.viewobjects.FxVulnerabilityAreaQueryParams;
import com.css.zfzx.fxpg.modules.vulnerability.province.repository.entity.FxVulnerabilityProvinceEntity;
import com.css.zfzx.fxpg.modules.vulnerability.province.service.FxVulnerabilityProvinceService;
import com.css.zfzx.fxpg.modules.vulnerability.province.viewobjects.FxVulnerabilityProvinceQueryParams;
import com.css.bpm.platform.utils.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author leon
 * @date 2020-11-10
 */
@Slf4j
@RestController
@RequestMapping("/fx/fxVulnerabilityProvinces")
public class FxVulnerabilityProvinceController {
    @Autowired
    private FxVulnerabilityProvinceService fxVulnerabilityProvinceService;

    @GetMapping("/queryFxVulnerabilityProvinces")
    public RestResponse queryFxVulnerabilityProvinces(HttpServletRequest request, FxVulnerabilityProvinceQueryParams queryParams) {
        RestResponse response = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = fxVulnerabilityProvinceService.queryFxVulnerabilityProvinces(queryParams, curPage, pageSize);
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 查询列表不分页
     *
     * @param queryParams
     * @return
     */
    @GetMapping("/listFxVulnerabilityProvinces")
    public RestResponse listFxVulnerabilityProvinces(FxVulnerabilityProvinceQueryParams queryParams) {
        RestResponse response = null;
        try {
            List<FxVulnerabilityProvinceEntity> fxVulnerabilityProvinceEntities = fxVulnerabilityProvinceService.listFxVulnerabilityProvinces(queryParams);
            response = RestResponse.succeed(fxVulnerabilityProvinceEntities);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("{id}")
    public RestResponse getFxVulnerabilityProvince(@PathVariable String id) {
        RestResponse response = null;
        try {
            FxVulnerabilityProvinceEntity fxVulnerabilityProvince = fxVulnerabilityProvinceService.getFxVulnerabilityProvince(id);
            response = RestResponse.succeed(fxVulnerabilityProvince);
        } catch (Exception e) {
            String errorMessage = "获取失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @PostMapping
    public RestResponse saveFxVulnerabilityProvince(@RequestBody FxVulnerabilityProvinceEntity fxVulnerabilityProvince) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            fxVulnerabilityProvinceService.saveFxVulnerabilityProvince(fxVulnerabilityProvince);
            json.put("message", "新增成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "新增失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;

    }

    @PutMapping
    public RestResponse updateFxVulnerabilityProvince(@RequestBody FxVulnerabilityProvinceEntity fxVulnerabilityProvince) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            fxVulnerabilityProvinceService.updateFxVulnerabilityProvince(fxVulnerabilityProvince);
            json.put("message", "修改成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @DeleteMapping
    public RestResponse deleteFxVulnerabilityProvinces(@RequestParam List<String> ids) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            fxVulnerabilityProvinceService.deleteFxVulnerabilityProvinces(ids);
            json.put("message", "删除成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("/getValidDictItemsByDictCode/{dictCode}")
    public RestResponse getValidDictItemsByDictCode(@PathVariable String dictCode) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(fxVulnerabilityProvinceService.getValidDictItemsByDictCode(dictCode));
        } catch (Exception e) {
            String errorMsg = "字典项获取失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

    /**
     * 导入
     *
     * @param file
     * @return
     */
    @PostMapping("/import")
    public RestResponse importExcel(@RequestParam("file") MultipartFile file) {
        RestResponse restResponse = null;
        try {
            String s = fxVulnerabilityProvinceService.importExcel(file);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errorMsg = "导入失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

    /**
     * 导出
     *
     * @param queryParams
     * @param response
     * @return
     */
    @GetMapping("/export")
    public RestResponse exportExcel(FxVulnerabilityProvinceQueryParams queryParams, HttpServletResponse response) {
        RestResponse restResponse = null;
        try {
            String s = fxVulnerabilityProvinceService.exportExcel(queryParams, response);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errorMsg = "导出失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

    /**
     * 条件查询省易损性数据（非分页）
     *
     * @param queryParams
     * @return
     */
    @GetMapping("/findFxVulnerabilityProvinces")
    public RestResponse findFxVulnerabilityProvince(FxVulnerabilityProvinceQueryParams queryParams) {
        RestResponse restResponse = null;
        try {
            List<FxVulnerabilityProvinceEntity> result = fxVulnerabilityProvinceService.findFxVulnerabilityProvince(queryParams);
            restResponse = RestResponse.succeed(result);
        } catch (Exception e) {
            String errorMsg = "查询省易损性数据失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

    /**
     * 初始化31个省
     *
     * @return
     */
    @GetMapping("/initialize")
    public RestResponse initialize() {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            fxVulnerabilityProvinceService.initialize();
            json.put("message", "初始化成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = e.getMessage();
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
}